package com.yeeKa.common.response;


import com.yeeKa.enums.StatusCode;

import java.io.Serializable;

/**
 * 通用的结果返回类
 *
 * @param <T>
 * @author win10
 */
public class ResponseResult<T> implements Serializable {


    private Integer code;

    private String message;

    private Object data;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public static ResponseResult<?> errorResult(int code, String msg) {
        ResponseResult<?> result = new ResponseResult<>();
        return result.error(code, msg);
    }

    public static ResponseResult<?> okResult(int code, String msg) {
        ResponseResult<?> result = new ResponseResult<>();
        return result.ok(code, null, msg);
    }

    public ResponseResult<?> okkResult(Object data) {
        ResponseResult<?> result = setStatusCode(StatusCode.SUCCESS, StatusCode.SUCCESS.getMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult<?> okResult(Object data) {
        ResponseResult<?> result = setStatusCode(StatusCode.SUCCESS, StatusCode.SUCCESS.getMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResponseResult<?> errorResult(StatusCode enums) {
        return setStatusCode(enums, enums.getMessage());
    }

    public static ResponseResult<?> errorResult(StatusCode enums, String message) {
        return setStatusCode(enums, message);
    }

    public static ResponseResult<?> setStatusCode(StatusCode enums) {
        return okResult(enums.getCode(), enums.getMessage());
    }

    private static ResponseResult<?> setStatusCode(StatusCode enums, String message) {
        return okResult(enums.getCode(), message);
    }

    public static ResponseResult<?> result(boolean b) {
        return okResult(b ? StatusCode.SUCCESS.getCode() : StatusCode.ERROR.getCode(),
                b ? StatusCode.SUCCESS.getMessage() : StatusCode.ERROR.getMessage());
    }

    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

//    public String getHost() {
//        return host;
//    }
//
//    public void setHost(String host) {
//        this.host = host;
//    }
}